/***************************************************************************\
 * The Mattise Kernel: Userland Software									*
 * Copyright 2007 Matthew Iselin											*
 * Licensed under the GPL													*
 *																			*
 * main.c																	*
 *																			*
 * Entrypoint for the test program											*
 *																			*
\***************************************************************************/

/***************************************************************************
 * Defines
 ***************************************************************************/
#define TESTING // testing message passing via userspace heap
#define FBOFFSET( x, y ) ( (y * 80 + x) * 2 )

/***************************************************************************
 * Includes
 ***************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <mmsg.h>
#include <vfs.h>
#include <process.h>
#include <portio.h>

/***************************************************************************
 * Functions
 ***************************************************************************/

// Entry point
int main( int argc, char* argv[] )
{
	// get the framebuffer
	__asm__ __volatile__ ( "int $0x80" : : "a" (34) );

	// valid, so we're set...
	*((short*) 0xb8000) = (0x0b << 8) | 'b';

	while( 1 );

	// tell the VFS we're here...
	struct vfs_Action* msg = (struct vfs_Action*) mattiseAllocMessageSz( sizeof( struct vfs_Action ) );
	msg->msgcode = VFS_MOUNT;
	msg->nmlen = sprintf( msg->filenm, "/tmp/testprog" );
	msg->vfsArg1 = msg->vfsArg2 = 0;
	msg->vfsArg3 = getpid();

	// send it
	mattiseSendMessage( 0, VFS_MOUNT, (char*) msg, sizeof( struct vfs_Action ) );

	// valid, so we're set...
	*((short*) 0xb8000) = (0x07 << 8) | 'c';

	// wait for a message
	while( !mattiseHasMessage() );

	// get it
	struct RecvMsgData myMsg;
	char* mbuff = mattiseRecvMessage( &myMsg );

	// test the pointer
	if( !mbuff )
	{
		*((short*) 0xb8000) = (0x07 << 8) | 'A';
		while( 1 );
	}

	// valid, so we're set...
	*((short*) 0xb8000) = (0x07 << 8) | 'Z';

	// free the message
	mattiseFreeMessage( mbuff );
	
	// loop forever
	while( 1 );
	
/*	// start up
	printf( "[TRPOG] I'm %d\n", getpid() );
	
	// testing...
	char* buff = (char*) malloc( 512 );
	int desc = -1;
	while( desc == -1 )
	{
		printf( "[TPROG] Open!\n" );
		desc = open( "/dev/dr0", 0, O_RDWR );
		printf( "Descriptor is %d\n", desc );
	}
	printf( "[TPROG] Descriptor is %d\n", desc );
	lseek( desc, 0, SEEK_SET );
	struct stat sh;
	fstat( desc, &sh );
	printf( "/dev/dr0 blocks is %d\n", sh.st_blocks );
	int bytcnt = read( desc, buff, 512 );
	printf( "[TPROG] Read %d bytes [%s]\n", bytcnt, buff );
	close( desc );
	free( buff );
	
	// get and print the current time
	time_t result;
    result = time( NULL );
	printf( "The time is now %s\n", ctime( &result ) );
	
	printf( "[TPROG] I'm done!\n" );
	
	// return success
	return 0;*/
}
